package com.junhai.tamsys.action;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import cn.myvsp.client.vdb.ResultSet;
import cn.myvsp.client.vdb.ResultSet.SortType;
import cn.myvsp.client.vdb.ResultSet.JoinType;
import cn.myvsp.client.vdb.Session;
import cn.myvsp.client.vdb.datasource.DataSource;
import cn.myvsp.webvsp.domain.search.Query.ListPage;
import cn.myvsp.client.vdb.impl.*;
import cn.myvsp.client.vdb.impl.FieldType.*;

import com.junhai.tamsys.factory.BeansFactory;
import com.junhai.tamsys.util.StringUtils;
import com.junhai.tamsys.util.CollectionUtils;
import com.opensymphony.xwork2.ActionSupport;

public class EntityAction extends ActionSupport{
	
	private String entityName;
	private ListPage listPage;
	private int pageNO;
	private int pageSize;
	List[] arrayList = new List[5];
	public DateFormat mediumFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
	
	public String getEntityName() {
		return entityName;
	}
	public void setEntityName(String entityName) {
		this.entityName = entityName;
	}
	public ListPage getListPage() {
		return listPage;
	}
	public void setListPage(ListPage listPage) {
		this.listPage = listPage;
	}
	public int getPageNO() {
		return pageNO;
	}
	public void setPageNO(int pageNO) {
		this.pageNO = pageNO;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
	
	public String query(){
		DataSource dataSource = (DataSource) BeansFactory.getBean("vdbDataSource");
		Session session;
		List<String> queryList1 = null;
		List<Long> queryList2 = null;
		ResultSet rs1 = null;
		ResultSet rs2 = null;
		String sql1 = null;
		StringBuffer sql2 = null;
		int count = 0;
		try{
			session = dataSource.getSession();
			queryList1 = StringUtils.SplitString(entityName);
			
			Map ePKeyMap1 = session.executeQuery("Entity_information","entityname",new StringType(),queryList1, "pkey",new LongType());              
			System.out.println("ePKeyMap1--->" + ePKeyMap1.toString());

			queryList2 = CollectionUtils.LMapToLList(ePKeyMap1);
			System.out.println("queryList2--->" + queryList2.toString());
			
			sql1 = "relentity1=" + queryList2.get(0);
			rs1 = session.executeQuery("Entity_relationInfo", "relentity2", sql1);
			
			rs1.sortBy("relationdegree", SortType.DESC);
			
			System.out.println("rs1'size---" + rs1.size());
			
			sql2 = new StringBuffer();
			sql2.append("pkey=");
			while(rs1.next()){
				count++;
				if(count > 8){
					break;
				}
				sql2.append( rs1.getLong("relentity2") + ";" );
			}
			sql2.append("-1");
			rs2 = session.executeQuery("Entity_information", "entityname", sql2.toString());
			System.out.println("rs2'size---" + rs2.size());
			
			
			
			
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
		
		return "success";
	}
	
	
}
